﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;

/// <summary>
/// 管理所有登录用户
/// </summary>
public class TUser1 : TData
{
    //常量
    const string CTableName = "t_user1";
    const string CKeyName = "userid";
    //与数据表字段对应的变量
    private string FUserID;
    private string FPWD;
    private string FUserName;
    private string FGrpCode;
    private int FLvl;
    private int FStatus;
    private string FInMan;
    private DateTime FInTime;
    private int FIId;

    public TUser1()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    public TUser1(string userid)
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
        FKeyValue = userid;
    }

    public TUser1(System.Web.UI.Page page)
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
        FKeyValue = (string)page.Session["userid"];
    }

    //根据uid获得记录值
    public override void Select()
    {
        string strSql = GetSelectString();
        TSqlDB oDB = new TSqlDB(strSql);
        if (!oDB.EOF)
        {
            FUserID = oDB["userid"].ToString();
            FPWD = oDB["pwd"].ToString();
            FUserName = oDB["username"].ToString();
            FGrpCode = oDB["grpcode"].ToString();
            FLvl = oDB["lvl"].ToInt();
            FStatus = oDB["status"].ToInt();
            FInMan = oDB["inman"].ToString();
            FInTime = oDB["intime"].ToDateTime();
            FIId = oDB["iid"].ToInt();
        }
        oDB.Free();
    }

    //插入新记录。
    public void Insert()
    {
        string ExSql = "'" + FUserID + "','" + FPWD + "','" + FUserName + "','" + FGrpCode + "'," + FLvl.ToString() + ",";
        ExSql += FStatus.ToString() + ",'" + FInMan + "',getdate()";
        base.Insert(ExSql);
        TUser oUser = new TUser();

        oUser.EMail = FUserID;
        oUser.PassWord = FPWD;
        oUser.BigName = FUserName;
        //---
        oUser.UserName = "";
        oUser.MyName = "";
        oUser.MySex = "";
        oUser.MyCompany = "";
        oUser.MyDept = "";
        oUser.MyPic = "";
        oUser.MyIntro = "";
        oUser.Status = 1;//未验证
        oUser.Insert();
        
    }

    //更新记录。
    public void Update()
    {
        string ExSql = "username='" + FUserName + "',grpcode='" + FGrpCode + "',lvl=" + FLvl.ToString() + ",status=" + FStatus.ToString() + ",intime=getdate()";
        base.Update(ExSql);
    }

    //
    public void Update_status()
    {
        string ExSql = "status=" + FStatus.ToString(); ;
        base.Update(ExSql);
    }

    //静态，可以不用创建对象即可调用。
    public static void DeleteByID(string userid)
    {
        string sSql = "delete t_user_access where userid='" + userid + "'";
        TSqlDB.ExecuteNonQuery(sSql);

        TData.DeleteByID(userid, CTableName, CKeyName);
    }

    //初始化密码为1234
    public static void ResetPWD(string userid)
    {
        string ExSql = "update " + CTableName + " set pwd='" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("1234", "MD5") + "' where userid='" + userid + "'";
        TSqlDB.ExecuteNonQuery(ExSql);
    }

    //初始化密码为自定义
    public static void ResetPWD2(string userid, string newpwd)
    {
        string ExSql = "update " + CTableName + " set pwd='" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(newpwd, "MD5") + "' where userid='" + userid + "'";
        TSqlDB.ExecuteNonQuery(ExSql);
    }

    //更改密码
    public void ChangePWD(string sPWD)
    {
        string ExSql = "pwd='" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sPWD, "MD5") + "'";
        base.Update(ExSql);
    }

    /// <summary>
    /// （静态）私有：根据用户名和密码检查用户身份
    /// 用户名和密码正确则返回UID，否则返回“”
    /// </summary>
    private static string CheckLoginReturnUID(string useruid, string userpwd)
    {
        useruid = useruid.Trim();
        userpwd = userpwd.Trim();

        string Result = "";
        bool isValid = false;

        // 验证数据库用户，并取UID
        string strSql = "select count(userid) from t_user1 where status=0 and userid='" + useruid + "' and pwd='" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(userpwd, "MD5") + "'";
        if (TSqlDB.ExecuteScalar(strSql) > 0)
        {
            Result = useruid;
        }

        // 返回结果
        return Result;
    }

    /// <summary>
    /// （静态）根据用户名和密码检查用户身份
    /// 用户名和密码正确则返回“真”，否则返回“假”
    /// </summary>
    public static bool CheckLogin(string useruid, string userpwd)
    {
        bool Result = false;
        string UserID = CheckLoginReturnUID(useruid, userpwd);
        Result = UserID.Length > 0;

        // 返回结果
        return Result;
    }

    /// <summary>
    /// （静态）根据用户名和密码检查用户身份
    /// 用户名和密码正确则返回“真”，否则返回“假”，并将UID保存在Session中
    /// </summary>
    public static bool CheckLogin(string useruid, string userpwd, System.Web.UI.Page page)
    {
        bool Result = false;
        string UserID = CheckLoginReturnUID(useruid, userpwd);
        Result = UserID.Length > 0;

        // 保存Session
        if (Result)
        {
            string sSql = "select username,grpcode,lvl from t_user1 where userid='" + UserID + "'";
            TSqlDBReader oReader = new TSqlDBReader(sSql);
            oReader.FDataReader.Read();
            page.Session["userid"] = UserID;
            page.Session["username"] = oReader.FDataReader.GetValue(0).ToString();
            page.Session["grpcode"] = oReader.FDataReader.GetValue(1).ToString();
            page.Session["lvl"] = oReader.FDataReader.GetValue(2).ToString();
            oReader.Free();
        }
        // 返回结果
        return Result && TUser.CheckLogin(useruid, userpwd, page);
    }

    //检查用户授权
    public static bool CheckGrant(string funccode, string uid, System.Web.UI.Page page)
    {
        bool result = false;

        string ip = page.Request.UserHostAddress;

        string sSql = "select gfcode from t_user_access where userid='" + uid + "' and gfcode='" + funccode + "' union ";
        sSql += "select funccode from t_user_grantfunc where funccode='" + funccode + "' and grpcode in (select gfcode from t_user_access where userid='" + uid + "')";
        TSqlDBReader oReader = new TSqlDBReader(sSql);
        if (oReader.FDataReader.HasRows)
        {
            TUserLog oLog = new TUserLog();
            oLog.UserID = uid;
            oLog.IPAddress = ip;
            oLog.FuncCode = funccode;
            oLog.Insert();

            result = true;
        }

        return result;
    }

    //检查使用期限
    public static bool CheckUseDate()
    {
        bool result = false;

        DateTime LimitDate = System.Convert.ToDateTime("2010-12-31");
        if (System.DateTime.Today <= LimitDate)
        {
            result = true;
        }

        return result;
    }

    /// <summary>
    ///（静态）检查用户是否是合法用户
    /// </summary>
    public static void CheckUser(System.Web.UI.Page page)
    {
        bool Result = false;

        TUser1 oUser = new TUser1(page);
        Result = !oUser.isNull;

        if (!Result)
            page.Server.Transfer("~\\login.aspx");
    }

    public bool isNull	// 各项属性是否为空
    {
        get
        {
            return (FKeyValue == null) || (FKeyValue == "");
        }
    }

    public string UserID
    {
        get
        {
            return FUserID;
        }
        set
        {
            FUserID = value;
        }
    }

    public string PWD
    {
        get
        {
            return FPWD;
        }
        set
        {
            FPWD = value;
        }
    }

    public string UserName
    {
        get
        {
            return FUserName;
        }
        set
        {
            FUserName = value;
        }
    }

    public string GrpCode
    {
        get
        {
            return FGrpCode;
        }
        set
        {
            FGrpCode = value;
        }
    }

    public int Lvl
    {
        get
        {
            return FLvl;
        }
        set
        {
            FLvl = value;
        }
    }

    public int Status
    {
        get
        {
            return FStatus;
        }
        set
        {
            FStatus = value;
        }
    }

    public string InMan
    {
        get
        {
            return FInMan;
        }
        set
        {
            FInMan = value;
        }
    }

    public DateTime InTime
    {
        get
        {
            return FInTime;
        }
        set
        {
            FInTime = value;
        }
    }

    public int IId
    {
        get
        {
            return FIId;
        }
        set
        {
            FIId = value;
        }
    }
}

/// <summary>
/// 管理系统中的功能，为授权操作提供信息
/// </summary>
public class TUserFunc : TData
{
    //常量
    const string CTableName = "t_user_func";
    const string CKeyName = "funccode";
    //与数据表字段对应的变量
    private string FFuncCode;
    private string FFuncName;
    private int FStatus;
    private decimal FSortNum;
    private int FIId;

    public TUserFunc()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    public TUserFunc(string funccode)
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
        FKeyValue = funccode;
    }

    //获得记录值
    public override void Select()
    {
        string strSql = GetSelectString();
        TSqlDB oDB = new TSqlDB(strSql);
        if (!oDB.EOF)
        {
            FFuncCode = oDB["funccode"].ToString();
            FFuncName = oDB["funcname"].ToString();
            FStatus = oDB["status"].ToInt();
            FSortNum = oDB["sortnum"].ToDecimal();
            FIId = oDB["iid"].ToInt();
        }
        oDB.Free();
    }

    //插入新记录。
    public void Insert()
    {
        string ExSql = "'" + FFuncCode + "','" + FFuncName + "'," + FStatus.ToString() + "," + FSortNum.ToString();
        base.Insert(ExSql);
    }

    //更新记录。
    public void Update()
    {
        string ExSql = "funcname='" + FFuncName + "',status=" + FStatus.ToString() + ",sortnum=" + FSortNum.ToString();
        base.Update(ExSql);
    }

    //静态，可以不用创建对象即可调用。
    public static void DeleteByID(string funccode)
    {
        TData.DeleteByID(funccode, CTableName, CKeyName);
    }

    public string FuncCode
    {
        get
        {
            return FFuncCode;
        }
        set
        {
            FFuncCode = value;
        }
    }

    public string FuncName
    {
        get
        {
            return FFuncName;
        }
        set
        {
            FFuncName = value;
        }
    }

    public int Status
    {
        get
        {
            return FStatus;
        }
        set
        {
            FStatus = value;
        }
    }

    public decimal SortNum
    {
        get
        {
            return FSortNum;
        }
        set
        {
            FSortNum = value;
        }
    }

    public int IId
    {
        get
        {
            return FIId;
        }
        set
        {
            FIId = value;
        }
    }
}

/// <summary>
/// 管理用户的分组
/// </summary>
public class TUserGroup : TData
{
    //常量
    const string CTableName = "t_user_group";
    const string CKeyName = "grpcode";
    //与数据表字段对应的变量
    private string FGrpCode;
    private string FGrpName;
    private int FStatus;
    private decimal FSortNum;
    private int FIId;

    public TUserGroup()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    public TUserGroup(string grpcode)
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
        FKeyValue = grpcode;
    }

    //获得记录值
    public override void Select()
    {
        string strSql = GetSelectString();
        TSqlDB oDB = new TSqlDB(strSql);
        if (!oDB.EOF)
        {
            FGrpCode = oDB["grpcode"].ToString();
            FGrpName = oDB["grpname"].ToString();
            FStatus = oDB["status"].ToInt();
            FSortNum = oDB["sortnum"].ToDecimal();
            FIId = oDB["iid"].ToInt();
        }
        oDB.Free();
    }

    //插入新记录。
    public void Insert()
    {
        string ExSql = "'" + FGrpCode + "','" + FGrpName + "'," + FStatus.ToString() + "," + FSortNum.ToString();
        base.Insert(ExSql);
    }

    //更新记录。
    public void Update()
    {
        string ExSql = "grpname='" + FGrpName + "',status=" + FStatus.ToString() + ",sortnum=" + FSortNum.ToString();
        base.Update(ExSql);
    }

    //静态，可以不用创建对象即可调用。
    public static void DeleteByID(string grpcode)
    {
        TData.DeleteByID(grpcode, CTableName, CKeyName);
    }

    public string GrpCode
    {
        get
        {
            return FGrpCode;
        }
        set
        {
            FGrpCode = value;
        }
    }

    public string GrpName
    {
        get
        {
            return FGrpName;
        }
        set
        {
            FGrpName = value;
        }
    }

    public int Status
    {
        get
        {
            return FStatus;
        }
        set
        {
            FStatus = value;
        }
    }

    public decimal SortNum
    {
        get
        {
            return FSortNum;
        }
        set
        {
            FSortNum = value;
        }
    }

    public int IId
    {
        get
        {
            return FIId;
        }
        set
        {
            FIId = value;
        }
    }
}

/// <summary>
/// 管理系统中权限组的划分
/// </summary>
public class TUserGrantGroup : TData
{
    //常量
    const string CTableName = "t_user_grantgroup";
    const string CKeyName = "grpcode";
    //与数据表字段对应的变量
    private string FGrpCode;
    private string FGrpName;
    private int FStatus;
    private decimal FSortNum;
    private int FIId;

    public TUserGrantGroup()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    public TUserGrantGroup(string grpcode)
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
        FKeyValue = grpcode;
    }

    //获得记录值
    public override void Select()
    {
        string strSql = GetSelectString();
        TSqlDB oDB = new TSqlDB(strSql);
        if (!oDB.EOF)
        {
            FGrpCode = oDB["grpcode"].ToString();
            FGrpName = oDB["grpname"].ToString();
            FStatus = oDB["status"].ToInt();
            FSortNum = oDB["sortnum"].ToDecimal();
            FIId = oDB["iid"].ToInt();
        }
        oDB.Free();
    }

    //插入新记录。
    public void Insert()
    {
        string ExSql = "'" + FGrpCode + "','" + FGrpName + "'," + FStatus.ToString() + "," + FSortNum.ToString();
        base.Insert(ExSql);
    }

    //更新记录。
    public void Update()
    {
        string ExSql = "grpname='" + FGrpName + "',status=" + FStatus.ToString() + ",sortnum=" + FSortNum.ToString();
        base.Update(ExSql);
    }

    //静态，可以不用创建对象即可调用。
    public static void DeleteByID(string grpcode)
    {
        TData.DeleteByID(grpcode, CTableName, CKeyName);
    }

    public string GrpCode
    {
        get
        {
            return FGrpCode;
        }
        set
        {
            FGrpCode = value;
        }
    }

    public string GrpName
    {
        get
        {
            return FGrpName;
        }
        set
        {
            FGrpName = value;
        }
    }

    public int Status
    {
        get
        {
            return FStatus;
        }
        set
        {
            FStatus = value;
        }
    }

    public decimal SortNum
    {
        get
        {
            return FSortNum;
        }
        set
        {
            FSortNum = value;
        }
    }

    public int IId
    {
        get
        {
            return FIId;
        }
        set
        {
            FIId = value;
        }
    }
}

/// <summary>
/// 管理权限组中的功能，父级表是t_user_grantgroup
/// </summary>
public class TUserGrantFunc : TData
{
    //常量
    const string CTableName = "t_user_grantfunc";
    const string CKeyName = "id";
    //与数据表字段对应的变量
    private int FID;
    private string FGrpCode;
    private string FFuncCode;

    public TUserGrantFunc()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    //获得记录值
    public override void Select()
    {
    }

    //插入新记录。
    public void Insert()
    {
        string ExSql = "insert into " + CTableName + " (grpcode,funccode) values('" + FGrpCode + "','" + FFuncCode + "')";
        TSqlDB.ExecuteNonQuery(ExSql);
    }

    //静态，可以不用创建对象即可调用。
    public static void DeleteByID(int id)
    {
        string ExSql = "delete " + CTableName + " where id=" + id.ToString();
        TSqlDB.ExecuteNonQuery(ExSql);
    }

    public static void DeleteByItem(string grpcode, string funccode)
    {
        string ExSql = "delete " + CTableName + " where grpcode='" + grpcode + "' and funccode='" + funccode + "'";
        TSqlDB.ExecuteNonQuery(ExSql);
    }

    public int ID
    {
        get
        {
            return FID;
        }
        set
        {
            FID = value;
        }
    }

    public string GrpCode
    {
        get
        {
            return FGrpCode;
        }
        set
        {
            FGrpCode = value;
        }
    }

    public string FuncCode
    {
        get
        {
            return FFuncCode;
        }
        set
        {
            FFuncCode = value;
        }
    }
}

/// <summary>
/// 管理用户可对系统存取的权限：整个权限组或单独的系统功能
/// </summary>
public class TUserAccess : TData
{
    //常量
    const string CTableName = "t_user_access";
    const string CKeyName = "id";
    //与数据表字段对应的变量
    private int FID;
    private string FUserID;
    private string FGFCode;

    public TUserAccess()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    //获得记录值
    public override void Select()
    {
    }

    //插入新记录。
    public void Insert()
    {
        string sSql = "select count(*) from " + CTableName + " where userid='" + FUserID + "' and gfcode='" + FGFCode + "'";
        if (TSqlDB.ExecuteScalar(sSql) == 0)
        {
            string ExSql = "insert into " + CTableName + " (userid,gfcode) values('" + FUserID + "','" + FGFCode + "')";
            TSqlDB.ExecuteNonQuery(ExSql);
        }
    }

    //静态，可以不用创建对象即可调用。
    public static void DeleteByItem(string userid, string gfcode)
    {
        string ExSql = "delete " + CTableName + " where userid='" + userid + "' and gfcode='" + gfcode + "'";
        TSqlDB.ExecuteNonQuery(ExSql);
    }

    public int ID
    {
        get
        {
            return FID;
        }
        set
        {
            FID = value;
        }
    }

    public string UserID
    {
        get
        {
            return FUserID;
        }
        set
        {
            FUserID = value;
        }
    }

    public string GFCode
    {
        get
        {
            return FGFCode;
        }
        set
        {
            FGFCode = value;
        }
    }
}

/// <summary>
/// 管理后台用户登录信息，记录模块的使用情况
/// </summary>
public class TUserLog : TData
{
    //常量
    const string CTableName = "t_user_log";
    const string CKeyName = "iid";
    //与数据表字段对应的变量
    private int FIID;
    private string FUserID;
    private string FIPAddress;
    private DateTime FLoginTime;
    private string FFuncCode;

    public TUserLog()
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    //获得记录值
    public override void Select()
    {
    }

    //插入新记录。
    public void Insert()
    {
        string ExSql = "insert into " + CTableName + " (userid,ipaddress,logintime,funccode) values('" + FUserID + "','" + FIPAddress + "',getdate(),'" + FFuncCode + "')";
        TSqlDB.ExecuteNonQuery(ExSql);
    }

    //静态，可以不用创建对象即可调用。
    public static void DeleteByItem(string iid)
    {
        string ExSql = "delete " + CTableName + " where iid=" + iid;
        TSqlDB.ExecuteNonQuery(ExSql);
    }

    public int IID
    {
        get
        {
            return FIID;
        }
        set
        {
            FIID = value;
        }
    }

    public string UserID
    {
        get
        {
            return FUserID;
        }
        set
        {
            FUserID = value;
        }
    }

    public string IPAddress
    {
        get
        {
            return FIPAddress;
        }
        set
        {
            FIPAddress = value;
        }
    }

    public DateTime LoginTime
    {
        get
        {
            return FLoginTime;
        }
        set
        {
            FLoginTime = value;
        }
    }

    public string FuncCode
    {
        get
        {
            return FFuncCode;
        }
        set
        {
            FFuncCode = value;
        }
    }
}

